const express = require('express');
const router = express.Router();

const Teacher = require("../models/teacher.js");
const Student = require("../models/student.js");
const Class = require("../models/class.js");
const Notice = require("../models/notice.js");
const Check = require("../modules/check.js")
//通过登录验证后进入主页
router.get("/homepage", Check.login, function (req, res, next) {
  Notice.find().then(function () {
    var notices = Notice.find().sort({ publishDate: -1 });
    var noticeTotal = Notice.find().count();
    Promise.all([notices, noticeTotal]).then(function (result) {
      res.render("homepage", {
        user: req.session.user,
        notices: result[0],
        noticeTotal: result[1],
      })
    }).catch(function (err) {
      res.json({ code: 201, message: err });
    })
  }).catch(function (err) {
    res.json({ code: 201, message: err });
  })


})

//进入校规页面
router.get("/rule", Check.login, function (req, res, next) {
  res.render("rule", {
    user: req.session.user,
    account: req.session.user.account,
  })
})
//进入修改密码
router.get("/alterpwd", Check.login, function (req, res, next) {
  res.render("users/alterpwd", {
    user: req.session.user,

    account: (req.session.user.identity == 0) ? req.session.user.teacherNo : req.session.user.studentNo,
  })
})
//修改密码
router.post("/alterpwd", Check.login, function (req, res, next) {
  var account = req.body.account;
  var password = req.body.password;

  var status = '';
  status = (req.session.user.identity == 0) ? Teacher : Student;
  status.alterpwd({ account, password }, function (err, result) {
    if (err) {
      res.json({ code: 201, message: err.message })
      return
    }
    res.json({ code: 200, message: "修改成功" })
  })
})
//退出登录
router.get('/logout', function (req, res, next) {
  req.session.user = null;
  res.clearCookie('account');
  res.redirect('/');
})
//——————————————————————————————————————————————————————————————————————————————
//进入个人资料/学生信息页面
router.get("/persondata", function (req, res, next) {
  if (req.session.user.identity == 1) {
    res.render("users/persondata.ejs", {
      account: req.session.user.account,
      user: req.session.user
    })
  }

  if (req.session.user.identity == 0) {
    var filter = { studentNo: req.query.studentNo };
    Student.find().then(function () {
      var studentmsg = Student.find(filter)
      Promise.all([studentmsg]).then(function (result) {
        res.render("users/persondata.ejs", {
          account: req.session.user.account,
          user: req.session.user,
          stumsg: result[0][0],
        })
      }).catch(function (err) {
        res.json({ code: 201, message: err });
      })
    }).catch(function (err) {
      res.json({ code: 201, message: err });
    })
  }


})
//修改个人资料
router.post("/alterdata", Check.login, function (req, res, next) {
  var data = req.body.data;

  Student.alterdata(data, function (err, result) {
    if (err) {
      res.json({ code: 201, message: err.message })
      return
    } else {
      req.session.user = result.message;
      res.json({ code: 200, message: "修改成功" })
    }
  })
})


router.get("/stuclassmsg", Check.login, function (req, res, next) {

  var classmsg = { className: req.session.user.className }//根据班级名称查询班级信息
  var studentcount = { classNameId: req.session.user.classNameId }//根据班级id查询学生
  Class.find().then(function () {
    var classmsg = Class.find(classmsg);
    var personcount = Student.find(studentcount).count();
    var studentmsg = Student.find(studentcount);
    Promise.all([classmsg, personcount, studentmsg]).then(function (result) {

      res.render("stuclassmsg", {
        user: req.session.user,
        classmsg: result[0][0],
        personcount: result[1],
        studentmsg: JSON.stringify(result[2]),
   
      })
    }).catch(function (err) {
      res.json({ code: 201, message: err });
    })
  }).catch(function (err) {
    res.json({ code: 201, message: err });
  })



})



router.get("/exam", Check.login, function (req, res, next) {
  res.render("users/exam.ejs", {
    account: req.session.user.account,
    teacherId: req.session.user._id,
    user: req.session.user,
  })
})
router.get("/post", Check.login, function (req, res, next) {
  res.render("users/post", {
    account: req.session.user.account,
    teacherId: req.session.user._id,
    user: req.session.user,
  })
})
//获得班级信息
router.post("/getclassMsg", Check.login, function (req, res, next) {
  var filter = { className: req.body.className };
  Class.find().then(function () {
    var classmsg = Class.find(filter);
    var personcount = Student.find(filter).count();
    Promise.all([classmsg, personcount]).then(function (result) {

      res.json({ code: 200, message: result })
    }).catch(function (err) {
      res.json({ code: 201, message: err });
    })
  }).catch(function (err) {
    res.json({ code: 201, message: err });
  })
})
//获得学生信息
router.post("/getstudentMsg", Check.login, function (req, res, next) {
  var filter = { className: req.body.className };

  Student.find().then(function () {
    var studentmsg = Student.find(filter)
    Promise.all([studentmsg]).then(function (result) {
      res.json({ code: 200, message: result })
    }).catch(function (err) {
      res.json({ code: 201, message: err });
    })
  }).catch(function (err) {
    res.json({ code: 201, message: err });
  })
})

module.exports = router;

